package com.alogic.es.xscript.search;

import com.alogic.xscript.ExecuteWatcher;
import com.alogic.xscript.Logiclet;
import com.alogic.xscript.LogicletContext;
import com.alogic.xscript.doc.XsObject;
import com.alogic.xscript.plugins.Segment;
import com.anysoft.util.Properties;
import com.anysoft.util.PropertiesConstants;

/**
 * query by compound bool
 *
 * @since 1.6.13.9 [20200824 duanyy]
 */
public class EsBool extends Segment {
    protected String $occur = "must";
    protected String $boost = "1.0";

    public EsBool(String tag, Logiclet p) {
        super(tag, p);
    }

    @Override
    public void configure(Properties p){
        super.configure(p);
        $occur = PropertiesConstants.getRaw(p,"occur",$occur);
        $boost = PropertiesConstants.getRaw(p,"boost",$boost);
    }

    protected void onExecute(XsObject root, XsObject current, LogicletContext ctx, ExecuteWatcher watcher) {
        String occur = PropertiesConstants.transform(ctx,$occur,"must");
        XsObject query = current.getObjectChild("bool", true);
        query.addProperty("boost", PropertiesConstants.transform(ctx,$boost,1.0f));
        XsObject f = query.getObjectChild(occur,true);
        super.onExecute(root,f,ctx,watcher);
    }
}